package com.woniuxy.carType.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class BookMvcConfig implements WebMvcConfigurer {
    @Value("${portal.allowed-origins}")
    private String[] allowOrigins;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  //允许所有访问地址
                .allowedOrigins(allowOrigins) //允许哪些服务器地址
                .allowedMethods("*")   //允许的请求类型
                .allowedHeaders("*")   //允许所有的请求头
                .allowCredentials(true)  //允许与凭据一起发送请求，Cookie可以跨域
                .exposedHeaders("*");   //允许返回的头信息
    }


    @Value("${portal.image-path}")
    private String imagePath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("图片地址: {}", imagePath);
        registry.addResourceHandler("/images/**").addResourceLocations(imagePath);
    }
}
